<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use \QCloud_WeApp_SDK\Auth\LoginService as LoginService;
use QCloud_WeApp_SDK\Constants as Constants;

class Base extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
    }

    public function userCheck()
    {
        $result = LoginService::check();
        if($result['loginState'] === Constants::E_AUTH){
            $this->error('微信未登录');
        }
        $userinfo = $result['userinfo'];

        $user = $this->user_model->getWechatUser($userinfo['openId']);
        if(empty($user)){
            $user = $this->user_model->insertUser($userinfo);
        }

        return $user;
    }
    public function normal($code = 200, $msg = 'success', $data = array(), $extra = array())
    {
        header('Content-type:application/json');
        $result = array('code' => $code, 'msg' => $msg);
        if ($data !== false) {
            $result['data'] = $data;
            if (!empty($extra)) {
                $result = array_merge($result, $extra);
            }
        }
        echo json_encode($result);
    }

    public function success($data = false)
    {
        $this->normal(200,'success',$data);
    }

    public function error($msg = 'error')
    {
        $this->normal(400,$msg);
        exit;
    }

}